import AluGeoUtil from '@/model/geometry/utils/AluGeoUtil';
import { CreateMeta, HoleMeta } from '@/model/meta/MetaClass';
import difference from '@turf/difference';
import { polygon } from '@turf/helpers';
import { Path, Vector2 } from 'three';
import {StringUtil} from "../../utils/StringUtil";
import PolygonUtil from "../../utils/Polygon";



/**
 *  老色皮(L)封板
 */
export default class LSPEntity  {

     //模型构件
     static __create(data: any): GeometryReturnMeta<Array<any>> {

        let vec = [];
        let indices = [];


        let bb = this.__basicBoard(data);
        let cc = this.__activeBoard(data);

        vec.push(...bb.vertices, ...cc.vertices);
        indices.push(...bb.indices, ...cc.indices.map((value) => { value += bb.vertices.length / 3; return value; }))

        this.setDuan(data);

        return {
            vertices: vec,
            indices: indices,
            meshface: []
        }
    }

    private static setDuan(data: any) {
        data["duan"] = [
            0, 0, 0,
            data.W, 0, 0,
            data.W, data.L, 0,
            data.W, data.L / 2, 0,
            0, data.L, 0,
            0, 0, data.H,
            0, data.L / 2, data.H,
            0, data.L, data.H

        ];
        data["zhong"] = {
            L: [
                {
                    pointer: [data.W / 2, data.L, 0],
                    center: [data.W / 2, data.L / 2, 0]
                },
                {
                    pointer: [0, data.L, data.H / 2],
                    center: [0, data.L / 2, data.H / 2]
                }
            ],
            RL: [
                {
                    pointer: [data.W / 2, 0, 0],
                    center: [data.W / 2, data.L / 2, 0]
                },
                {
                    pointer: [0, 0, data.H / 2],
                    center: [0, data.L / 2, data.H / 2]
                }
            ],
            // W: [
            //     {
            //         pointer: [data.W, data.L / 2, 0],
            //         center: [data.W / 2, data.L / 2, 0]
            //     },
            // ],
            // RW: [
            //     {
            //         pointer: [0, data.L / 2, 0],
            //         center: [data.W / 2, data.L / 2, 0]
            //     },
            // ]
        }
    }


    /** x轴板*/
    private static __basicBoard(data: any): GeometryReturnMeta<Array<any>> {

        let l = +data.L, w = +data.W, t = +data.bkT;

        // let back = [
        //     0, 0, 0, w, 0, 0, w, l, 0, 0, l, 0,
        // ];

        // let front = [
        //     0, 0, t, w, 0, t, w, l, t, 0, l, t,
        // ];

        /** 直径1*/
        // let holeStr = data.p_r_pos1;
        // let hole0 = StringUtil.formStringToArray(holeStr);

        let hole0 = this.filterHoles1(data, "p_r_pos1", (+data.p_r_d1) * 0.5);



        /** 直径2*/
        // let holeStr1 = data.p_r_pos2;
        // let hole1 = StringUtil.formStringToArray(holeStr1);

        let hole1 = this.filterHoles1(data, "p_r_pos2", (+data.p_r_d2) * 0.5);


        /** 直径3*/
        // let holeSt2 = data.p_r_pos3;
        // let hole2 = StringUtil.formStringToArray(holeSt2);

        let hole2 = this.filterHoles1(data, "p_r_pos3", (+data.p_r_d3) * 0.5);


        let hole = [hole0, hole1, hole2];
        let arcR = [(+data.p_r_d1) * 0.5, (+data.p_r_d2) * 0.5, (+data.p_r_d3) * 0.5];
        let path = new Path();
        let rs = [];

        for (let i: number = 0; i < hole.length; i++) {
            let h = hole[i];
            let rr = arcR[i];
            for (let j: number = 0; j < h.length; j += 2) {
                let p = path.clone();
                let x = +h[j];
                let y = +h[j + 1];
                p.moveTo(x, y).absarc(x, y, rr, 0.0, 2 * Math.PI, false);
                rs.push(p);
            }
        }




        //缺口参数

        let yqkwd = 0
        let yqkgd = 0;
        let yqkwoff = 0;
        let yqkloff = 0;


        let f = polygon([[
            [0, 0],
            [w, 0],
            [w, l],
            [0, l],
            [0, 0]
        ]])

        //左缺口的参数
        let start = w - yqkwoff;
        let p1 = polygon([
            [
                [start, yqkloff],
                [start, yqkloff + yqkgd],
                [start - yqkwd, yqkloff + yqkgd],
                [start - yqkwd, yqkloff],
                [start, yqkloff],
            ]
        ])

        let r = difference(f, p1).geometry.coordinates;

        let back = null;
        let front = null;
        let extra = [];
        for (let i: number = 0; i < r.length; i++) {
            let b = [];
            let f = [];

            let gg = r[i];
            for (let j: number = 0; j < gg.length - 1; j++) {
                b.push(...gg[j], 0);
                f.push(...gg[j], t);
            }


            if (i == 0) {
                back = b;
                front = f;
            }
            else {
                extra.push([b, f]);
            }
        }



        let h = new HoleMeta(data.BKDia * 0.5, w - data.fjcmjl, data.Ezybkk, "y", rs, extra);
        let c: CreateMeta = new CreateMeta(t, front, back, h);

        return AluGeoUtil.createBoardWithHole(c);
    }



    private static filterHoles(data: any, key: string, d: number) {
        let str = data[key];

        let zqkwd = 0;
        let zqkgd = 0;
        let zqkwoff = 0;
        let zqkloff = 0;


        let w = +data.W


        let start = w - zqkwoff - zqkwd;
        let r1 = [
            new Vector2(start, zqkloff),
            new Vector2(start + zqkwd, zqkloff),
            new Vector2(start + zqkwd, zqkloff + zqkgd),
            new Vector2(start, zqkloff + zqkgd),
        ];

        if (!!str) {
            let r = []
            let hole = StringUtil.formStringToArray(str);

            for (let i: number = 0; i < hole.length; i += 2) {
                let x = hole[i];
                let y = hole[i + 1];

                let rr = [
                    new Vector2(x - d, y - d),
                    new Vector2(x + d, y - d),
                    new Vector2(x + d, y + d),
                    new Vector2(x - d, y + d),
                ]

                let isInter = PolygonUtil.intersectRect(rr, r1);
                if (!isInter) {
                    r.push(x, y);
                }
            }

            return r;
        }

        return [];
    }


    private static filterHoles1(data: any, key: string, d: number) {
        let str = data[key];

        let zqkwd = 0;
        let zqkgd = 0;
        let zqkwoff = 0;
        let zqkloff = 0;

        let w = +data.W


        let start = w - zqkwoff - zqkwd;
        let r1 = [
            new Vector2(start, zqkloff),
            new Vector2(start + zqkwd, zqkloff),
            new Vector2(start + zqkwd, zqkloff + zqkgd),
            new Vector2(start, zqkloff + zqkgd),
        ];

        if (!!str) {
            let r = []
            let hole = StringUtil.formStringToArray(str);

            for (let i: number = 0; i < hole.length; i += 2) {
                let x = hole[i];
                let y = hole[i + 1];

                let rr = [
                    new Vector2(x - d, y - d),
                    new Vector2(x + d, y - d),
                    new Vector2(x + d, y + d),
                    new Vector2(x - d, y + d),
                ]

                let isInter = PolygonUtil.intersectRect(rr, r1);
                if (!isInter) {
                    r.push(x, y);
                }
            }

            return r;
        }

        return [];
    }

    /** 创建z轴板*/
    private static __activeBoard(data: any): GeometryReturnMeta<Array<any>> {

        let l = +data.L, w = +data.H, t = +data.bkT;

        let rr = (+data.angle) * -1 / 180 * Math.PI;
        let cr = Math.cos(rr);
        let sr = Math.sin(rr);

        // let back = [
        //     0, 0, 0, w, 0, 0, w, l, 0, 0, l, 0,
        // ];

        // let front = [
        //     0, 0, t, w, 0, t, w, l, t, 0, l, t,
        // ];

        let zqkwd = 0;
        let zqkgd = 0;
        let zqkwoff =0;
        let zqkloff = 0;


        let f = polygon([[
            [0, 0],
            [w, 0],
            [w, l],
            [0, l],
            [0, 0]
        ]])

        //左缺口的参数
        let start = w - zqkwoff;
        let p1 = polygon([
            [
                [start, zqkloff],
                [start, zqkloff + zqkgd],
                [start - zqkwd, zqkloff + zqkgd],
                [start - zqkwd, zqkloff],
                [start, zqkloff],
            ]
        ])

        let r = difference(f, p1).geometry.coordinates;

        let back = null;
        let front = null;
        let extra = [];
        for (let i: number = 0; i < r.length; i++) {
            let b = [];
            let f = [];

            let gg = r[i];
            for (let j: number = 0; j < gg.length - 1; j++) {
                b.push(...gg[j], 0);
                f.push(...gg[j], t);
            }


            if (i == 0) {
                back = b;
                front = f;
            }
            else {
                extra.push([b, f]);
            }
        }



        // let h = new HoleMeta(25, 200, [100, 300, 500]);
        /** 直径1*/
        // let holeStr = data.p_l_pos1;
        // let hole0 = StringUtil.formStringToArray(holeStr);

        let hole0 = this.filterHoles(data, "p_l_pos1", (+data.p_l_d1) * 0.5);


        /** 直径2*/
        // let holeStr1 = data.p_l_pos2;
        // let hole1 = StringUtil.formStringToArray(holeStr1);

        let hole1 = this.filterHoles(data, "p_l_pos2", (+data.p_l_d2) * 0.5);

        /** 直径3*/
        // let holeSt2 = data.p_l_pos3;
        // let hole2 = StringUtil.formStringToArray(holeSt2);

        let hole2 = this.filterHoles(data, "p_l_pos3", (+data.p_l_d3) * 0.5);


        let hole = [hole0, hole1, hole2];
        let arcR = [(+data.p_l_d1) * 0.5, (+data.p_l_d2) * 0.5, (+data.p_l_d3) * 0.5];
        let path = new Path();
        let rs = [];

        for (let i: number = 0; i < hole.length; i++) {
            let h = hole[i];
            let rr = arcR[i];
            for (let j: number = 0; j < h.length; j += 2) {
                let p = path.clone();
                let x = +h[j];
                let y = +h[j + 1];
                p.moveTo(x, y).absarc(x, y, rr, 0.0, 2 * Math.PI, false);
                rs.push(p);
            }
        }



        let h = new HoleMeta(data.BKDia * 0.5, w - data.fjcmjl, data.Ezybkk, "y", rs, extra);
        let c: CreateMeta = new CreateMeta(t, front, back, h);
        let result = AluGeoUtil.createBoardWithHole(c)

        for (let i: number = 2; i < result.vertices.length; i += 3) {
            result.vertices[i] -= t;
        }


        for (let i: number = 0; i < result.vertices.length; i += 3) {
            let x = result.vertices[i];
            let z = result.vertices[i + 2];

            result.vertices[i] = x * cr + sr * z;
            result.vertices[i + 2] = cr * z - x * sr;
        }

        return result;
    }
}